package org.bee.tl.core;

import java.io.UnsupportedEncodingException;

public abstract class ByteSupportTag extends Tag {
	protected byte[] inputBytes = null;
	protected boolean byteContent = false;
	public static final byte[] NULL_BYTE = new byte[0];

	public void setByteInput(byte[] bs) {
		this.inputBytes = bs;
		byteContent = true;
	}

	public String getInput() {
		if (input != null) {
			return this.input;
		} else if (inputBytes != null) {
			try {
				return new String(inputBytes, group.getCharset());
			} catch (UnsupportedEncodingException e) {
				throw new RuntimeException(e.getMessage());
			}
		} else {
			return null;
		}
	}

	public byte[] getInputAsBytes() {
		if (inputBytes != null) {
			return inputBytes;
		} else if (input != null) {
			try {
				return input.getBytes(group.getCharset());
			} catch (UnsupportedEncodingException e) {
				throw new RuntimeException(e.getMessage());
			}
		} else {
			return null;
		}
	}

	public boolean isByteContent() {
		return byteContent;
	}

	public void setByteContent(boolean byteContent) {
		this.byteContent = byteContent;
	}

	@Override
	public String getOutput() {
		try {
			return new String(this.getOutputAsByte(), group.getCharset());
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e.getMessage());
		}
	}

	public abstract byte[] getOutputAsByte();
}
